package com.sherllinhe.member.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sherllinhe.common.context.LoginMemberContext;
import com.sherllinhe.common.resp.CommonResp;
import com.sherllinhe.common.resp.PageResp;
import com.sherllinhe.common.util.SnowUtil;
import com.sherllinhe.member.domain.Passenger;
import com.sherllinhe.member.domain.PassengerExample;
import com.sherllinhe.member.mapper.PassengerMapper;
import com.sherllinhe.member.req.PassengerQueryReq;
import com.sherllinhe.member.req.PassengerSaveReq;
import com.sherllinhe.member.resp.PassengerQueryResp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 功能：
 * 作者：Sherllin He
 * 日期：2025/5/7 16:48
 */
@Slf4j
@Service
public class PassengerService {
    @Autowired
    private PassengerMapper passengerMapper;
    // 新增和编辑乘客
    public void save(PassengerSaveReq req){
        DateTime now = DateTime.now();
        Passenger passenger = BeanUtil.copyProperties(req, Passenger.class);

        //判断是新增还是更新
        passenger.setMemberId(LoginMemberContext.getId());
        if(ObjUtil.isNull(passenger.getId())) {
            //新增
            PassengerExample  example = new PassengerExample();
            example.createCriteria().andMemberIdEqualTo(LoginMemberContext.getId());
            if(passengerMapper.countByExample(example) <= 50){
                passenger.setId(SnowUtil.getSnowflakeNextId());
                passenger.setCreateTime(now);
                passenger.setUpdateTime(now);
                passengerMapper.insert(passenger);
            }else{
                throw new RuntimeException("最多添加50个乘客");
            }

        }else{
            //更新
            passenger.setUpdateTime(now);
            passengerMapper.updateByPrimaryKey(passenger);
        }


    }
    // 查询乘客列表
    public PageResp<PassengerQueryResp> queryList(PassengerQueryReq req){
        PassengerExample example = new PassengerExample();
        example.setOrderByClause("update_time desc");
        PassengerExample.Criteria criteria = example.createCriteria();
        if(ObjUtil.isNotNull(req.getMemberId())){
            criteria.andMemberIdEqualTo(req.getMemberId());
        }

        log.info("查询页码:{}",req.getPage());
        log.info("每页条数:{}",req.getSize());
        PageHelper.startPage(req.getPage(),req.getSize());
        List<Passenger> passengerList = passengerMapper.selectByExample(example);

        PageInfo<Passenger> pageInfo = new PageInfo<>(passengerList);
        log.info("总条数:{}", pageInfo.getTotal());
        log.info("总页数:{}", pageInfo.getPages());

        List<PassengerQueryResp> list = BeanUtil.copyToList(passengerList, PassengerQueryResp.class);

        PageResp<PassengerQueryResp> pageResp = new PageResp<>();
        pageResp.setList(list);
        pageResp.setTotal(pageInfo.getTotal());

        return pageResp;
    }
    //删除乘客
    public void delete(Long id){
        passengerMapper.deleteByPrimaryKey(id);
    }

    /*
     * 查询我的所有乘客
     * */
    public List<PassengerQueryResp> queryMine(){
        PassengerExample example = new PassengerExample();
        example.setOrderByClause("name asc");
        PassengerExample.Criteria criteria = example.createCriteria();
        criteria.andMemberIdEqualTo(LoginMemberContext.getId());
        List<Passenger> list = passengerMapper.selectByExample(example);
        return BeanUtil.copyToList(list, PassengerQueryResp.class);
    }
}
